package com.example.capsule_chat0322.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.capsule_chat0322.R;
import com.example.capsule_chat0322.bean.MessageBean;
import com.example.capsule_chat0322.bean.UserInfoBean;
import com.example.capsule_chat0322.global.GlobalAssistMap;
import com.example.capsule_chat0322.request.RoomRequest;
import com.example.capsule_chat0322.utils.AlterUtils;

/**
 * @Author zhuzhiming
 * @Date 2022/5/20 0:33
 */
public class AddFriendActivity extends AppCompatActivity {

    private EditText friend_id;
    private TextView sender_name;
    private EditText add_instruction;
    private Button add_commit;
    private final static int ADD_SECRET_ROOM = 1;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_friend);
        initView();
    }


    private void initView() {

        friend_id = findViewById(R.id.friend_id);
        sender_name = findViewById(R.id.sender_name);
        add_instruction = findViewById(R.id.add_instruction);
        add_commit = findViewById(R.id.add_commit);

        sender_name.setText("添加好友");

        String friendId = getIntent().getStringExtra("friendId");
        if (friendId != null) {
            friend_id.setText(friendId);
        }

        add_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                doCommit();
            }
        });

    }

    private void doCommit() {

        String userId = (String) GlobalAssistMap.getObject("userId");
        if (userId == null) {
            try {
                Thread.sleep(50);
                userId = (String) GlobalAssistMap.getObject("userId");
                if (userId == null) {
                    Thread.sleep(100);
                    userId = (String) GlobalAssistMap.getObject("userId");
                    if (userId == null) {
                        Thread.sleep(500);
                        userId = (String) GlobalAssistMap.getObject("userId");
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        if (userId == null) {
            return;
        }

        MessageBean messageBean = new MessageBean();
        String friendId = friend_id.getText().toString();
        String instruction = add_instruction.getText().toString();
        if (friendId.length() == 0) {
            return;
        }

        if (friendId.equals(userId)) {
            AlterUtils.alertByToastInMainThread(AddFriendActivity.this, "不能加自己");
            return;
        }

        if (!instruction.equals("") && instruction.length() > 16) {
            AlterUtils.alertByToastInMainThread(AddFriendActivity.this, "申请说明不可以大于16个字符");
            return;
        }

        if (!instruction.equals("") && instruction.contains("->")) {
            AlterUtils.alertByToastInMainThread(AddFriendActivity.this, "申请说明不可以包含->特殊符号");
            return;
        }

        if (GlobalAssistMap.getObject(friendId + "position") != null) {
            AlterUtils.alertByToastInMainThread(AddFriendActivity.this, "对方已经是您的好友");
            return;
        }

        messageBean.setSenderId(userId);
        messageBean.setReceiverId(friendId);
        messageBean.setContent(instruction);
        UserInfoBean userInfoBean = (UserInfoBean) GlobalAssistMap.getObject("userInfo");
        messageBean.setSenderAvatarPath(userInfoBean.getAvatarUrl());

        RoomRequest.addFriend(messageBean, new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(@NonNull Message message) {
                MessageBean messageBean = (MessageBean) message.obj;
                int status = messageBean.getStatus();
                if (status == 0 || status == 1) {
                    AlterUtils.alertByToastInMainThread(AddFriendActivity.this, "申请成功~等待对方回复~");
                    finish();
                } else {
                    friend_id.setText("");
                    AlterUtils.alertByToastInMainThread(AddFriendActivity.this, "不存在该好友");
                }
                return true;
            }
        }));

    }
}
